<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Woreport extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');
		$this->load->helper('download');
		$this->load->helper('file');

	}
	public function index() {
		$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
		$this->load->view($this->session->userdata('formain'),array(
			'wocode' => $this->woreport_model->get_maintain_type()));
		$this->load->view('layouts/footer');
	}

	public function get_status() {
		$d1 = $this->input->post('year');
		$d2 = $this->input->post('month');
		$stat = $this->input->post('status');
		if(empty($d1) || empty($d2)){
			$date = '';
		}else{
			$date = $d1."-".$d2;
		}
		$query = $this->woreport_model->get_status($date,$stat);
		echo json_encode($query);
	}

	public function mail_window() {
		$this->load->view($this->session->userdata('formain') . '_mail', array('title' => "Email Form"));
	}

	public function gen_pdf() {
		$result = $this->collectiondetails_model->get_status($_GET['range1'], $_GET['range2'],$_GET['date_1'],$_GET['date_2'],$_GET['ref_1'],$_GET['ref_2']);
		//var_dump($result);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($result as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->receipt_date . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->receipt_number . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->acct_name . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="center">' . $key->status . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->pay_amount . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->reference . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Receipt Range From " . $_GET['range1'] . " to " . $_GET['range2'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
				 <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Payment Date From " . $_GET['date_1'] . " to " . $_GET['date_2'] . "</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=16%; padding: 5px;'>Date</td>
					<td style='border-bottom:1px solid; text-align: center;width=13%; padding: 5px;'>Receipt Number</td>
					<td style='border-bottom:1px solid; text-align: center;width=29%; padding: 5px;'>Account Name</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Status</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Bill Details</td>
					<td style='border-bottom:1px solid; text-align: center;width=14%; padding: 5px;'>Reference</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Collection Details Report');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('Collection_Details_' . date("dmY_His") . '.pdf', 'D');
	}

	public function gen_excel() {
		$range1 = 'null';
		$range2 = 'null';
		$d1 = 'null';
		$d2 ='null';
		if (!empty($_GET['range1'])) {
			$range1 = $_GET['range1'];
		}
		if (!empty($_GET['range2'])) {
			$range2 = $_GET['range2'];
		}
		if (!empty($_GET['date_1'])) {
			$d1 = $_GET['date_1'];
		}
		if (!empty($_GET['date_2'])) {
			$d2 = $_GET['date_2'];
		}
		//activate worksheet number 1
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		//name the worksheet
		$excel->getActiveSheet()->setTitle('Collection Details Report');
		//set cell A1 content with some text
		$excel->getActiveSheet()->setCellValue('A1', 'Receipt Range From ' . $range1 . ' to ' . $range2 .' AND Payment Date From '.$d1.' to '.$d2);
		$excel->getActiveSheet()->setCellValue('A3', 'Date');
		$excel->getActiveSheet()->setCellValue('B3', 'Receipt Number');
		$excel->getActiveSheet()->setCellValue('C3', 'Account Name');
		$excel->getActiveSheet()->setCellValue('D3', 'Status');
		$excel->getActiveSheet()->setCellValue('E3', 'Year');
		$excel->getActiveSheet()->setCellValue('F3', 'Bill Details');
		$excel->getActiveSheet()->setCellValue('G3', 'Reference');

		$result = $this->collectiondetails_model->get_status($_GET['range1'],$_GET['range2'],$_GET['date_1'],$_GET['date_2'],$_GET['ref_1'],$_GET['ref_2']);
		$cellnum = 4;

		foreach ($result as $key) {
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->receipt_date);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->receipt_number);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->acct_name);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->status);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->year);
			$excel->getActiveSheet()->setCellValue('F' . $cellnum, $key->pay_amount);
			$excel->getActiveSheet()->getStyle('F' . $cellnum)->getNumberFormat()->setFormatCode('###,###,###.00');
			$excel->getActiveSheet()->setCellValue('G' . $cellnum, $key->reference);
			$cellnum++;
		}
		//$excel->getActiveSheet()->getStyle('A1:F' . $cellnum = $cellnum - 1)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		// $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getFill()->getStartColor()->setARGB('29bb04');
		//       // Add some data
		//       $excel->getActiveSheet()->getStyle("A1:F1")->getFont()->setBold(true);
		//       $excel->getActiveSheet()->getStyle('A1:F1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->setFillType(PHPexcel_Style_Fill::FILL_SOLID);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getFill()->getStartColor()->setARGB('6F6F6F');
		// Add some data
		$excel->getActiveSheet()->getStyle("A3:G3")->getFont()->setBold(true);
		//$excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
		$excel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
		$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:F1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3:G3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'Collection_Details_Report_ ' . date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */